//
//  GYOrderGoodsCell.m
//  GoodLuck
//
//  Created by apple on 2018/3/20.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *orderGoodsCellIdentifier = @"orderGoodsCellIdentifier";


#import "GYOrderGoodsCell.h"
#import "GYGoodsTableViewCell.h"

@interface GYOrderGoodsCell ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *storeNameLbale;
@property (weak, nonatomic) IBOutlet UILabel *expressCostLable;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightCons;

@property (weak, nonatomic) IBOutlet UITextField *storeMsgTF;

@property (weak, nonatomic) IBOutlet UILabel *goodsSumPriceLable;

/** 商品数量 */
@property (nonatomic, strong) NSArray *goodsList;
@end

@implementation GYOrderGoodsCell
- (void)awakeFromNib {
    [super awakeFromNib];
    [self.storeMsgTF addTarget:self action:@selector(registerFirst) forControlEvents:UIControlEventEditingDidEndOnExit];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
}

- (void)registerFirst {
    [self.storeMsgTF resignFirstResponder];
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    GYOrderGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:orderGoodsCellIdentifier];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] firstObject];
    }
    
    return cell;
}

- (void)setStore:(GYConfirmOrderStore *)store {
    _store = store;
    
    
    //赋值
    self.storeNameLbale.text = store.store_name;
    
    self.expressCostLable.text = store.store_total_goods_freight == nil ? @"免运费" : [NSString stringWithFormat:@"¥%@",store.store_total_goods_freight];
    self.goodsSumPriceLable.text = [NSString stringWithFormat:@"¥%@",store.store_final_total_list];
    
    //设置刷新表格的一些操作
    self.tableViewHeightCons.constant = store.goods_list.count * 80;
    self.goodsList = store.goods_list;
    [self.tableView reloadData];
}

- (CGFloat)height:(GYConfirmOrderStore *)model {
    
    self.store = model;
    
    [self layoutIfNeeded];
    
    return CGRectGetMaxY(self.goodsSumPriceLable.frame) + 15;
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.goodsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYGoodsTableViewCell *cell = [GYGoodsTableViewCell cellWithTableView:tableView];
    cell.goods = self.goodsList[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //跳转到商品详情界面
    [self.storeMsgTF resignFirstResponder];
    self.seeGoodsDetailsCallBack([self.goodsList[indexPath.row] goods_id]);
}

#pragma mark -- UITextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.storemessageCallBack(textField.text);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.storeMsgTF resignFirstResponder];
}
@end
